package com.shujia.flink.core

import org.apache.flink.api.common.eventtime.{SerializableTimestampAssigner, WatermarkStrategy}
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time

import java.time.Duration


object Demo5EventTime {
  def main(args: Array[String]): Unit = {

    /**
     * java,1678756528000
     * java,1678756529000
     * java,1678756530000
     * java,1678756531000
     * java,1678756532000
     * java,1678756535000
     * java,1678756535000
     * java,1678756537000
     * java,1678756540000
     */
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(1)

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    //1、解析数据
    val eventDS: DataStream[(String, Long)] = linesDS.map(line => {
      val split: Array[String] = line.split(",")
      val word: String = split(0)
      //时间字段
      val ts: Long = split(1).toLong
      (word, ts)
    })

    //2、告诉flink那一个字段时时间字段
    //val assDS: DataStream[(String, Long)] = eventDS.assignAscendingTimestamps(kv => kv._2)

    val ws: WatermarkStrategy[(String, Long)] = WatermarkStrategy
      //指定数据最大的乱序时间，-- 水位线前移秒数
      .forBoundedOutOfOrderness[(String, Long)](Duration.ofSeconds(5))
      //指定时间字段
      .withTimestampAssigner(new SerializableTimestampAssigner[(String, Long)] {
        override def extractTimestamp(element: (String, Long), recordTimestamp: Long): Long = element._2
      })

    //指定时间字段和水位线
    val assDS: DataStream[(String, Long)] = eventDS.assignTimestampsAndWatermarks(ws)

    /**
     * 每隔5秒统计单词的数量
     *
     * TumblingEventTimeWindows:滑动的事件时间窗口
     *
     * 事件时间划分窗口的方式
     * 1、将一个整秒，按照窗口大小等分成多个窗口
     * 2、窗口计算的条件
     *    1、窗口内有数据
     *    2、大于等于窗口结束时间的数据达到之后会计算
     */

    assDS
      .map(kv => (kv._1, 1))
      .keyBy(_._1)
      .window(TumblingEventTimeWindows.of(Time.seconds(5)))
      .sum(1)
      .print()


    env.execute()

  }

}
